package popcorn.ticket.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.entity.Grades;
import popcorn.ticket.service.GradesService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@RestController
public class GradesController {
	@Autowired
	GradesService gradesService;

	// 分页
	@GetMapping(value = "gradespage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Grades> listBlogByPage1(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords) {
		// 直接返回分页的数据
		return gradesService.selectGradesByLike(1, 10, "");
	}

	// 修改
	@PostMapping(value = "updGrades", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public AjaxResponse updUsers(@RequestBody Grades users) {
		String msg = "修改失败";
		if (users != null) {
			int flag = gradesService.updateByPrimaryKeySelective(users);
			if (flag > 0) {
				msg = "修改成功";
			}
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
		return ajaxResponse;
	}

	// 增加
	@PostMapping(value = "addGrades", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public AjaxResponse addUsers(@RequestBody Grades users) {
		String msg = "添加失败";
		if (users != null) {
			int flag = gradesService.addGrades(users);
			if (flag > 0) {
				msg = "添加成功";
			}
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
		return ajaxResponse;
	}

	// 删除
	@PostMapping(value = "delGrades/{gradeid}", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public AjaxResponse delGrades(@PathVariable("gradeid") int gradeid) {
		String msg = "删除失败";
		if (gradeid > 0) {
			int flag = gradesService.delGradesById(gradeid);
			if (flag > 0) {
				msg = "删除成功";
			}
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
		return ajaxResponse;
	}

	// 获得会员名字
	@GetMapping(value = "getGreadeName", produces = "application/json;charset=utf-8")
	@ResponseBody
	// 参数位置获取前台传递的json值
	public List<Grades> getGreadeName() {

		return gradesService.getGrades();
	}
}
